/* eslint-disable eqeqeq *///忽略==的警告
import { atTime, atSunrise, atSunset, beforeSunrise, afterSunrise, beforeSunset, afterSunset, MaxTemp, MinTemp } from "../const/infoConst";
import presetDefault from "../const/presetFile"
import { getDeviceOccupyAreas } from "./sceneUtils";
import { lightControlShowSmartshiftOccu } from "./dailogUtils";
import { saveLightInfor, saveGroupSetInfor, saveAreaInfor } from './homeUtils'
import PubSub from 'pubsub-js'
import { meshLog, SPRemove, SPGet, SPSet } from "../config";
import { transIntToHex4Str, getOctets2Msg, transIntToHex2Str } from "./dataUtils";

export const initDefaultData = (smartshiftSet) => {
    let contail1 = false;
    let contail2 = false;
    let mType = 0;
    for (let i = 0; i < smartshiftSet.areaSet.length; i++) {
        if (smartshiftSet.areaSet[i].action == 1) {
            contail1 = true;
        } else if (smartshiftSet.areaSet[i].action == 2) {
            contail2 = true;
        }
    }
    if (contail1) {
        mType = 1;
    }
    if (contail2) {
        mType = 2;
    }
    if (smartshiftSet.smartType != mType) {
        smartshiftSet.smartType = mType;
        SPSet('smartshiftSetx', JSON.stringify(smartshiftSet));
    }
    meshLog("wjk mType:", mType);
}


export const getAreaName = (addr) => {
    let info = 'area';
    let areaSetx = JSON.parse(SPGet("areaSetx"));

    for (let i = 0; i < areaSetx.length; i++) {
        if (parseInt(areaSetx[i].areaNo, 16) == addr) {
            info = areaSetx[i].name;
            break;
        }
    }
    return info;
}

export const getSsidInfo = () => {
    let info = '';
    let deviceSetx = JSON.parse(SPGet("deviceSetDefault"));
    meshLog("wjk deviceSetx:", deviceSetx);

    for (let i = 0; i < deviceSetx.length; i++) {
        if (deviceSetx[i].pid === '100E') {
            info = deviceSetx[i].ssid;
        }
    }

    return info;
}

//数据转换为str,显示
export const showPresetTimeStr = (model) => {
    if (!model.hasOwnProperty('scheduleEvent')) {
        return '';
    }

    if (model.presetID == 0 && !model.scheduleEvent.isShow) {
        return '';
    }

    let title = '';
    if (model.scheduleEvent.itemType === atTime) {
        let hour = Math.floor(model.scheduleEvent.time / 60);
        let min = model.scheduleEvent.time % 60;

        let minStr = ''
        min < 10 ? (minStr = '0' + min) : (minStr = '' + min)

        if (hour == 0) {
            title = 12 + ':' + minStr + 'am'
        } else if (hour < 12) {
            title = hour + ':' + minStr + 'am'
        } else if (hour == 12) {
            title = 12 + ':' + minStr + 'pm'
        } else {
            title = (hour - 12) + ':' + minStr + 'pm'
        }
    } else if (model.scheduleEvent.itemType == atSunrise) {
        title = 'Sunrise';
    } else if (model.scheduleEvent.itemType == atSunset) {
        title = 'Sunset';
    } else if (model.scheduleEvent.itemType == beforeSunrise) {
        if (model.scheduleEvent.time == 0) {
            title = 'Sunrise';
        } else {
            title = model.scheduleEvent.time + ' Minute(s) before Sunrise';
            if (model.scheduleEvent.time % 60 == 0) {
                title = Math.floor(model.scheduleEvent.time / 60) + ' Hour(s) before Sunrise';
            }
        }
    } else if (model.scheduleEvent.itemType == afterSunrise) {
        if (model.scheduleEvent.time == 0) {
            title = 'Sunrise';
        } else {
            title = model.scheduleEvent.time + ' Minute(s) after Sunrise';
            if (model.scheduleEvent.time % 60 == 0) {
                title = Math.floor(model.scheduleEvent.time / 60) + ' Hour(s) after Sunrise';
            }
        }
    } else if (model.scheduleEvent.itemType == beforeSunset) {
        if (model.scheduleEvent.time == 0) {
            title = 'Sunset';
        } else {
            title = model.scheduleEvent.time + ' Minute(s) before Sunset';
            if (model.scheduleEvent.time % 60 == 0) {
                title = Math.floor(model.scheduleEvent.time / 60) + ' Hour(s) before Sunset';
            }
        }
    } else if (model.scheduleEvent.itemType == afterSunset) {
        if (model.scheduleEvent.time == 0) {
            title = 'Sunset';
        } else {
            title = model.scheduleEvent.time + ' Minute(s) after Sunset';
            if (model.scheduleEvent.time % 60 == 0) {
                title = Math.floor(model.scheduleEvent.time / 60) + ' Hour(s) after Sunset';
            }
        }
    }
    // meshLog("wjk title:",title);
    return title;
};

export const getSendMsg = () => {
    let smartshiftSet = JSON.parse(SPGet("smartshiftSetx"));
    let sunRise = 6 * 60;
    let sunSet = 18 * 60;
    let scheData = getPresetItemList(smartshiftSet.presetNo, smartshiftSet.presets);
    meshLog("wjk scheData", scheData);
    let info = "";
    let model;
    for (let i = 0; i < scheData.length; i++) {
        let str = getSmartshiftMsgData(i, 1, sunRise, sunSet, scheData[i]);
        info = info + str + "&";
        if (i == 0) {
            model = scheData[i];
        }
    }
    if (scheData.length < 16) {
        for (let i = scheData.length; i < 16; i++) {
            model.scheduleEvent.itemType = 0;
            model.scheduleEvent.time = 6 * 60;
            model.scheduleEvent.colorStatus.dim = 40;
            model.scheduleEvent.colorStatus.temperature = 3500;

            let str = getSmartshiftMsgData(i, 0, sunRise, sunSet, model);
            info = info + str + "&";
        }
    }
    meshLog("wjk info", info);

    return info;
}

//数据转换为msg,发送给设备
function getSmartshiftMsgData(mIndex, mEnable, sunRise, sunSet, model) {
    let mHour = 0;
    let mMin = 0;
    let mCCT = MinTemp;
    let mDIM = 1;

    let mTime = getTimeNum(model, sunRise, sunSet);
    mHour = getDiffHour(mTime / 60);
    mMin = mTime % 60;

    mCCT = model.scheduleEvent.colorStatus.temperature;
    mDIM = model.scheduleEvent.colorStatus.dim;
    if (mCCT < MinTemp) {
        mCCT = MinTemp;
    }
    if (mCCT > MaxTemp) {
        mCCT = MaxTemp;
    }

    if (mDIM < 1) {
        mDIM = 1;
    }
    if (mDIM > 100) {
        mDIM = 100;
    }


    let data1 = mMin * 4 + (mHour - mHour % 8) / 8;
    let data2 = (mHour % 8) * 32 + (mEnable * 16) + mIndex;
    let data3 = transIntToHex4Str(mCCT);

    let msg = transIntToHex2Str(data2) + transIntToHex2Str(data1) + getOctets2Msg(data3) + transIntToHex2Str(mDIM);
    // meshLog("wjk msg",msg);
    return msg;
}


function getTimeNum(model, sunRise, sunSet) {
    let itemTime = 0;
    if (model.scheduleEvent.itemType == atTime) {
        itemTime = parseInt(model.scheduleEvent.time);
    } else if (model.scheduleEvent.itemType == beforeSunrise) {
        itemTime = sunRise - parseInt(model.scheduleEvent.time);
    } else if (model.scheduleEvent.itemType == afterSunrise) {
        itemTime = sunRise + parseInt(model.scheduleEvent.time);
    } else if (model.scheduleEvent.itemType == beforeSunset) {
        itemTime = sunSet - parseInt(model.scheduleEvent.time);
    } else if (model.scheduleEvent.temType == afterSunset) {
        itemTime = sunSet + parseInt(model.scheduleEvent.time);
    } else if (model.scheduleEvent.itemType == atSunrise) {
        itemTime = sunRise;
    } else if (model.scheduleEvent.itemType == atSunset) {
        itemTime = sunSet;
    }
    // meshLog("wjk itemTime:",parseInt(itemTime));
    return parseInt(itemTime);
}



export const showCurTimeInfo = () => {
    let info = ''
    let time = new Date();
    let hour = time.getHours(); //时
    hour = getDiffHour(hour);
    let min = time.getMinutes();//分

    let minStr = ''
    min < 10 ? (minStr = '0' + min) : (minStr = '' + min)

    if (hour == 0) {
        info = 12 + ':' + minStr + 'am'
    } else if (hour < 12) {
        info = hour + ':' + minStr + 'am'
    } else if (hour == 12) {
        info = 12 + ':' + minStr + 'pm'
    } else {
        info = (hour - 12) + ':' + minStr + 'pm'
    }
    return info

}

export const showPresetName = (presetNo, presetSet) => {
    // meshLog("wjk presetNo:",presetNo,presetSet);
    let name = ''
    if (presetNo == 0 || presetNo == 1) {
        name = 'Wellness'
    } else if (presetNo == 2) {
        name = 'Nature'
    } else if (presetNo == 3) {
        name = 'Office'
    } else if (presetNo == 4) {
        name = 'School'
    } else if (presetNo == 5) {
        name = 'Calm'
    } else {
        presetSet.map((item) => {
            if (item.presetID === presetNo && item.scheduleEvent.itemNo === 0) {
                name = item.name;
            }
        })
    }
    // meshLog("wjk PresetName:",name);
    return name;
}

export const getPresetItemList = (presetNo, presetSet) => {
    if (presetNo == 0) {
        presetNo = 1;
    }
    let info = [];
    let count = 0;
    if (presetNo <= 5) {
        presetDefault.map((item) => {
            if (item.presetID == presetNo) {
                if (item.scheduleEvent.itemNo > 0) {
                    info[count] = item;
                    count++;
                }

            }
        })
    } else {
        let isSun = false;
        presetSet.map((item) => {
            if (item.presetID == presetNo && item.scheduleEvent.itemNo > 0) {
                if (item.scheduleEvent.itemType > 0) {
                    isSun = true;
                }
                info[count] = item;
                count++;
            }
        })

        //有日出日落，需要排序
        if (isSun) {
            attrSortData(info);
        }

    }
    meshLog("wjk info:", info);
    return info;
}


function attrSortData(info) {
    let smartshiftSet = JSON.parse(SPGet("smartshiftSetx"));
    let sunRise = 6 * 60;
    let sunSet = 18 * 60;
    // const arr = smartshiftSet.lnglat.replace('[', '').replace(']', '').split(',');
    // if(arr.length==2){
    //     let mlng=arr[0];
    //     let mlat=arr[1];
    //     sunRise=sunRiseAndsunSetInfo(true,mlng,mlat);;
    //     sunSet=sunRiseAndsunSetInfo(false,mlng,mlat);
    //     // meshLog('wjk 经度=',mlng,' 纬度=',mlat, ' 日出=',parseInt(sunRise/60),sunRise%60,' 日落=',parseInt(sunSet/60),sunSet%60);
    // }

    info.sort(compare(sunRise, sunSet)).reverse()

}

//排序
function compare(sunRise, sunSet) {
    return function (a, b) {
        var value1 = getTimeNum(a, sunRise, sunSet);
        var value2 = getTimeNum(b, sunRise, sunSet);
        // meshLog('wjk value1',parseInt(value1));
        return parseInt(value2) - parseInt(value1);
    }
}


export const showPresetTemColor = (tem) => {
    let info = '#E58943'
    if (tem <= 2200) {
        info = '#E58943'
    } else if (tem <= 3000) {
        info = '#F4AD76'
    } else if (tem <= 4000) {
        info = '#FEE396'
    } else if (tem <= 5000) {
        info = '#D6EEFF'
    } else {
        info = '#ACDCFE'
    }
    return info;
}

export const getZone = (lng) => {
    let zone = parseInt(lng / 15);
    let templng = lng * 100;
    if (templng > 0) {
        if (templng % 1500 > 750) {
            zone = zone + 1;
        }
    } else {
        if (templng % 1500 < -750) {
            zone = zone - 1;
        }
    }

    return zone;
}



export const sunRiseAndsunSetInfo = (isSunRise, mlng, mlat) => {
    let zenith = 90.833333;
    let localOffset = getZone(mlng);

    let time = new Date();
    let year = time.getFullYear();
    let month = time.getMonth() + 1;
    let day = time.getDate();
    // meshLog("wjk time:",year,month,day);
    //1.首先计算一年中的哪一天
    let N1 = Math.floor(275 * month / 9);
    let N2 = Math.floor((month + 9) / 12);
    let N3 = (1 + Math.floor((year - 4 * Math.floor(year / 4) + 2) / 3));
    let N = N1 - (N2 * N3) + day - 30;
    //2.将经度转换为小时值并计算近似时间
    let lngHour = mlng / 15;
    let t = 0;
    if (isSunRise) {
        t = N + ((6 - lngHour) / 24);
    } else {
        t = N + ((18 - lngHour) / 24);
    }
    //3.计算太阳的平均异常
    let M = (0.9856 * t) - 3.289;
    //4.计算太阳的真经度,L可能需要通过加/减360调整到范围[0-360)
    let L = M + (1.916 * Math.sin(M * (Math.PI / 180))) + (0.020 * Math.sin(2 * M * (Math.PI / 180))) + 282.634;
    while (L < 0) {
        L = L + 360;
    }
    while (L >= 360) {
        L = L - 360;
    }
    //5a.计算太阳的赤经,RA可能需要通过加/减360调整到范围[0-360)
    let RA = (180 / Math.PI) * Math.atan(0.91764 * Math.tan(L * (Math.PI / 180)));
    while (RA < 0) {
        RA = RA + 360;
    }
    while (RA >= 360) {
        RA = RA - 360;
    }
    //5b.赤经值必须与L在同一象限
    let Lquadrant = (Math.floor(L / 90)) * 90;
    let RAquadrant = (Math.floor(RA / 90)) * 90;
    RA = RA + (Lquadrant - RAquadrant);
    //5c.赤经值需要换算成小时
    RA = RA / 15;
    //6.计算太阳赤纬
    let sinDec = 0.39782 * Math.sin(L * (Math.PI / 180));
    let cosDec = Math.cos(Math.asin(sinDec));
    //7a.计算太阳的本地时角
    let cosH = (Math.cos(zenith * (Math.PI / 180)) - (sinDec * Math.sin(mlat * (Math.PI / 180)))) / (cosDec * Math.cos(mlat * (Math.PI / 180)));
    if (cosH > 1) {
        //太阳永远不会在这个地方升起（在指定的日期）
    } else if (cosH < -1) {
        //太阳永远不会落在这个地方（在指定的日期）
    }
    //7b.完成H的计算并换算成小时
    let H = 0;
    if (isSunRise) {
        H = 360 - (180 / Math.PI) * Math.acos(cosH);
    } else {
        H = (180 / Math.PI) * Math.acos(cosH);
    }
    H = H / 15;
    //8.计算本地平均上升/下降时间
    let T = H + RA - (0.06571 * t) - 6.622;
    //9.调整回UTC, UT可能需要通过加/减24调整到范围[0,24)
    let UT = T - lngHour;
    if (UT < 0) {
        UT = UT + 24;
    } else if (UT >= 24) {
        UT = UT - 24;
    }
    //10.将UT值转换为经纬度的本地时区
    let localT = UT + localOffset;
    if (localT < 0) {
        localT = localT + 24;
    } else if (localT >= 24) {
        localT = localT - 24;
    }
    let min = parseInt(Math.floor(localT * 60) % 60);
    let timeC = parseInt(localT) * 60 + min;
    // meshLog("wjk timeC:",timeC);
    return timeC;

}



export const getTemDimNumData = () => {
    let smartshiftSet = JSON.parse(SPGet("smartshiftSetx"));
    //日出日落
    let sunRise = 6 * 60;
    let sunSet = 18 * 60;
    // const arr = smartshiftSet.lnglat.replace('[', '').replace(']', '').split(',');
    // if(arr.length==2){
    //     let mlng=arr[0];
    //     let mlat=arr[1];
    //     sunRise=sunRiseAndsunSetInfo(true,mlng,mlat);;
    //     sunSet=sunRiseAndsunSetInfo(false,mlng,mlat);
    //     meshLog('wjk 经度=',mlng,' 纬度=',mlat, ' 日出=',parseInt(sunRise/60),sunRise%60,' 日落=',parseInt(sunSet/60),sunSet%60);
    // }

    let time = new Date();
    let mHour = time.getHours(); //时
    let mMin = time.getMinutes();//分

    let scheData = getPresetItemList(smartshiftSet.presetNo, smartshiftSet.presets);

    return getTemDimNumInfo(scheData, mHour, mMin, sunRise, sunSet);

}


function getTemDimNumInfo(scheData, mHour, mMin, sunRise, sunSet) {
    let getData = { dim: 60, tem: 2700 };

    let timePos = 0;
    mHour = getDiffHour(mHour);
    // meshLog('wjk mHour',mHour,' mMin',mMin,' total:',mHour*60+mMin);
    for (let i = 0; i < scheData.length; i++) {
        let itemTime = getTimeNum(scheData[i], sunRise, sunSet);
        // meshLog('wjk itemTime',itemTime);
        if (i == 0) {
            if (mHour * 60 + mMin <= itemTime) {
                timePos = -2;
            }
        } else if (i == scheData.length - 1) {
            if (mHour * 60 + mMin >= itemTime) {
                timePos = -1;
            }
        } else {
            if (mHour * 60 + mMin >= itemTime) {
                timePos = i;
            }
        }
    }
    // meshLog('wjk timePos',timePos);
    if (timePos == -2) {
        let model0 = scheData[0];
        let model1 = scheData[scheData.length - 1];
        getData = showDimTemOverUI(model0, model1, mHour, mMin, 24, sunRise, sunSet);
    } else if (timePos == -1) {
        let model0 = scheData[0];
        let model1 = scheData[scheData.length - 1];
        getData = showDimTemOverUI(model0, model1, mHour, mMin, 0, sunRise, sunSet);
    } else {
        if (timePos + 1 <= scheData.length - 1) {
            let model0 = scheData[timePos];
            let model1 = scheData[timePos + 1];
            getData = showDimTemInnerUI(model0, model1, mHour, mMin, sunRise, sunSet);
        }
    }

    return getData;
}

function showDimTemOverUI(model0, model1, mHour, mMin, num, sunRise, sunSet) {
    let dim0 = getTimeNum(model0, sunRise, sunSet);
    let dim1 = getTimeNum(model1, sunRise, sunSet);

    let dimNum = getRealNum(model0.scheduleEvent.colorStatus.dim * 655, model1.scheduleEvent.colorStatus.dim * 655, dim0 + 24 * 60, dim1, mHour + num, mMin) / 655;
    dimNum = parseInt(dimNum);
    if (dimNum < 0) {
        dimNum = -dimNum;
    }
    if (dimNum > 100) {
        dimNum = 100;
    }
    meshLog("wjk Over dimNum:", dimNum);

    let temNum = getRealNum(model0.scheduleEvent.colorStatus.temperature, model1.scheduleEvent.colorStatus.temperature, dim0 + 24 * 60, dim1, mHour + num, mMin);
    if (temNum < 0) {
        temNum = -temNum;
    }
    if (temNum < parseInt(MinTemp)) {
        temNum = parseInt(MinTemp);
    }
    if (temNum > parseInt(MaxTemp)) {
        temNum = parseInt(MaxTemp);
    }
    meshLog("wjk Over temNum:", temNum);
    return { dim: dimNum, tem: temNum };
}


function showDimTemInnerUI(model0, model1, mHour, mMin, sunRise, sunSet) {
    // meshLog("wjk Inner model0:",model0);
    // meshLog("wjk Inner model1:",model1);
    let dim0 = getTimeNum(model0, sunRise, sunSet);
    let dim1 = getTimeNum(model1, sunRise, sunSet);


    let dimNum = getRealNum(model1.scheduleEvent.colorStatus.dim * 655, model0.scheduleEvent.colorStatus.dim * 655, dim1, dim0, mHour, mMin) / 655;
    dimNum = parseInt(dimNum);
    if (dimNum < 0) {
        dimNum = -dimNum;
    }
    if (dimNum > 100) {
        dimNum = 100;
    }
    // meshLog("wjk Inner dimNum:",dimNum);

    let temNum = getRealNum(model1.scheduleEvent.colorStatus.temperature, model0.scheduleEvent.colorStatus.temperature, dim1, dim0, mHour, mMin);
    if (temNum < 0) {
        temNum = -temNum;
    }
    if (temNum < parseInt(MinTemp)) {
        temNum = parseInt(MinTemp);
    }
    if (temNum > parseInt(MaxTemp)) {
        temNum = parseInt(MaxTemp);
    }
    // meshLog("wjk Inner temNum:",temNum);

    return { dim: dimNum, tem: temNum };
}

function getRealNum(model1dim, model0dim, dim1, dim0, mHour, mMin) {
    model1dim = parseInt(model1dim);
    model0dim = parseInt(model0dim);
    // meshLog("wjk model1dim:",model1dim,' model0dim',model0dim,' dim1',dim1,' dim0',dim0,' mHour',mHour,' mMin',mMin);
    let dimCount = model1dim - model0dim;
    let dimTime = dim1 - dim0;

    // meshLog("wjk dimCount:",dimCount,' dimTime',dimTime,' mHour',mHour);
    let dimbit0 = parseInt(dimCount / dimTime);
    let dimbit1 = parseInt(dimCount % dimTime);
    let extDimTime = (mHour * 60 + mMin) - dim0;
    let dimNum = model0dim + extDimTime * dimbit0;
    // meshLog("wjk dimbit0:",dimbit0,' dimbit1',dimbit1,' extDimTime',extDimTime,' dimNum',dimNum);
    if (Math.abs(extDimTime) <= Math.abs(dimbit1)) {
        if (dimbit1 < 0) {
            dimNum = dimNum - extDimTime;
        } else {
            dimNum = dimNum + extDimTime;
        }
    } else {
        dimNum = dimNum + dimbit1;
    }
    // meshLog("wjk getRealNum:",parseInt(dimNum));
    return parseInt(dimNum);
}

function getDiffHour(mHour) {
    //时区偏移量
    let zoneDiff = getZoneDiff();

    if (zoneDiff < 24 && zoneDiff > -24) {
        mHour = mHour - zoneDiff;
    }
    if (mHour < 0) {
        mHour = mHour + 24;
    }
    if (mHour > 24) {
        mHour = mHour - 24;
    }

    return mHour;
}

function getZoneDiff() {
    let smartshiftSet = JSON.parse(SPGet("smartshiftSetx"));
    // meshLog("wjk diff ",smartshiftSet);

    let zoneNo = smartshiftSet.zoneNo;
    if (zoneNo == 0) {
        return 0;
    } else {
        let extra = 0;
        if (zoneNo == 1) {
            extra = -5;
            if (judgeXLS()) {
                extra = -4;
            }
        } else if (zoneNo == 2) {
            extra = -6;
            if (judgeXLS()) {
                extra = -5;
            }
        } else if (zoneNo == 3) {
            extra = -7;
            if (judgeXLS()) {
                extra = -6;
            }
        } else if (zoneNo == 4) {
            extra = -8;
            if (judgeXLS()) {
                extra = -7;
            }
        } else if (zoneNo == 5) {
            extra = -9;
            if (judgeXLS()) {
                extra = -8;
            }
        } else if (zoneNo == 6) {
            extra = -10;
        }

        let timeZone = smartshiftSet.location.timeZone;

        let num = timeZone - extra;
        if (num < -24) {
            num = num + 24;
        }
        if (num > 24) {
            num = num - 24;
        }
        if (num < -24 || num > 24) {
            num = 0;
        }
        return num;
    }
}

//计算时区信息
export const getZoneInfo = (smartshiftSet) => {  //zoneNo,timeZone,zone7Special
    let info = '';

    let zoneNo = smartshiftSet.zoneNo;
    let timeZone = smartshiftSet.location.timeZone;
    let zone7Special = smartshiftSet.zone7Special;

    if (zoneNo == 1) {
        if (judgeXLS()) {
            info = getSpecialZoneStr(-4, zone7Special);
        } else {
            info = getSpecialZoneStr(-5, zone7Special);
        }
    } else if (zoneNo == 2) {
        if (judgeXLS()) {
            info = getSpecialZoneStr(-5, zone7Special);
        } else {
            info = getSpecialZoneStr(-6, zone7Special);
        }
    } else if (zoneNo == 3) {
        if (judgeXLS()) {
            info = getSpecialZoneStr(-6, zone7Special);
        } else {
            info = getSpecialZoneStr(-7, zone7Special);
        }
    } else if (zoneNo == 4) {
        if (judgeXLS()) {
            info = getSpecialZoneStr(-7, zone7Special);
        } else {
            info = getSpecialZoneStr(-8, zone7Special);
        }
    } else if (zoneNo == 5) {
        if (judgeXLS()) {
            info = getSpecialZoneStr(-8, zone7Special);
        } else {
            info = getSpecialZoneStr(-9, zone7Special);
        }
    } else if (zoneNo == 6) {
        info = getSpecialZoneStr(-10, zone7Special);
    } else {
        if (timeZone <= -4 && timeZone >= -10) {
            info = 'Default-' + getSpecialZoneStr(timeZone, zone7Special);
        } else {
            if (timeZone < 0) {
                info = 'UTC' + timeZone + ':00';   //Default-
            } else {
                info = 'UTC+' + timeZone + ':00';  //Default-
            }
            if (timeZone == 0) { //新站点，只有默认值处理
                info = '';
            }
        }
    }
    meshLog("wjk info:", info);

    return info;
}


function getSpecialZoneStr(mNo, zone7Special) {
    let info = '';
    if (judgeXLS()) {
        if (mNo == -4) {
            info = 'UTC-4:00 Eastern Daylight Time';
        } else if (mNo == -5) {
            info = 'UTC-5:00 Central Daylight Time';
        } else if (mNo == -6) {
            info = 'UTC-6:00 Mountain Daylight Time';
        } else if (mNo == -7) {
            if (!zone7Special) {
                info = 'UTC-7:00 Pacific Daylight Time';
            } else {
                info = 'UTC-7:00 Mountain Standard Time';
            }
        } else if (mNo == -8) {
            info = 'UTC-8:00 Alaska Daylight Time';
        } else if (mNo == -9) {
            info = 'UTC-9:00';
        } else if (mNo == -10) {
            info = 'UTC-10:00 Hawaii-Aleutian Standard Time';
        }
    } else {
        if (mNo == -4) {
            info = 'UTC-4:00';
        } if (mNo == -5) {
            info = 'UTC-5:00 Eastern Standard Time';
        } else if (mNo == -6) {
            info = 'UTC-6:00 Central Standard Time';
        } else if (mNo == -7) {
            info = 'UTC-7:00 Mountain Standard Time';
        } else if (mNo == -8) {
            info = 'UTC-8:00 Pacific Standard Time';
        } else if (mNo == -9) {
            info = 'UTC-9:00 Alaska Standard Time';
        } else if (mNo == -10) {
            info = 'UTC-10:00 Hawaii-Aleutian Standard Time';
        }
    }

    return info;

}


//计算是否夏令时
//每年3月的第二个周日凌晨2AM
//每年11月的第一个周日凌晨2AM
function judgeXLS() {
    let time = new Date();
    let mYear = time.getFullYear(); //年
    let mWeek = time.getDay();
    let mTotal = time.getTime();
    // meshLog("wjk mYear:", mYear);
    // meshLog("wjk mWeek:", mWeek);


    let bTime = new Date(mYear + "-03-01");
    let bWeek = bTime.getDay();
    if (bWeek == 0) {
        bWeek = 7;
    }
    let bTotal = bTime.getTime() + (14 - bWeek) * 24 * 3600 * 1000;
    // meshLog("wjk bTotal:", bTotal);


    let eTime = new Date(mYear + "-11-01");
    let eWeek = eTime.getDay();
    if (eWeek == 0) {
        eWeek = 7;
    }
    let eTotal = eTime.getTime() + (7 - eWeek) * 24 * 3600 * 1000;
    // meshLog("wjk eTotal:", eTotal);

    if (mTotal > bTotal && mTotal < eTotal) {
        return true;
    } else {
        return false;
    }

}

//判断场景应用   false--发广播  true--多组控制
export const judgeSceneApplySendMutiGroup = (devices) => {
    if (devices.length <= 2) {
        return false;
    }

    let smartshiftSet = JSON.parse(SPGet("smartshiftSetx"));
    if (smartshiftSet.smartType == 0) {
        return false;
    } else {
        return true;
    }
}


//应用场景取消smartshift   应用Schedule取消smartshift
export const deviceControlJudgeAreaWorkOnSmartShift = (devices) => {
    let attrA = [];

    if (devices.length <= 2) {
        return attrA;
    }

    let smartshiftSet = JSON.parse(SPGet("smartshiftSetx"));
    let areaSet = smartshiftSet.areaSet;
    if (areaSet.length == 0 || smartshiftSet.smartType == 0) {
        return attrA;
    }


    let countA = 0;
    let areaArr = getDeviceOccupyAreas(devices); //device占用的area集合
    meshLog("wjk areaSet:", areaSet);
    for (let i = 0; i < areaSet.length; i++) {
        if (areaSet[i].action == 0) {
            continue;
        }

        let isOccu = false;
        for (let m = 0; m < areaArr.length; m++) {
            if (areaSet[i].address == parseInt(areaArr[m], 16) && areaSet[i].action > 0) {
                isOccu = true;
            }
        }
        if (isOccu) {
            attrA[countA] = areaSet[i].address;
            countA++;
        }
    }
    meshLog("wjk attrA:", attrA.length, attrA);

    return attrA;   //获取占用的area地址集合
}

//home界面控灯，取消对应area smartshift
export const lightControlJudgeAreaWorkOnSmartShift = (addr, isDim) => {
    // meshLog("wjk home界面控灯");
    let isOccu = false;
    if (typeof (addr) == 'undefined' || addr.length != 4) {
        return isOccu;
    }
    let areaNo = addr;
    if (parseInt(addr, 16) < parseInt('C000', 16)) {
        let deviceSetx = JSON.parse(SPGet("deviceSetx"));
        for (let n = 0; n < deviceSetx.length; n++) {
            if (deviceSetx[n].address == addr) {
                areaNo = deviceSetx[n].areaNo;
                break;
            }
        }
    } else if (addr.substring(3, 4) != '0') {
        areaNo = addr.substring(0, 3) + '0';
    }


    if (parseInt(areaNo, 16) < parseInt('C010', 16)) {
        return isOccu;  //过滤未分组
    }
    meshLog("wjk areaNo:", areaNo);

    let action = 0;
    if (isDim) {
        action = 1;
    }

    let smartshiftSet = JSON.parse(SPGet("smartshiftSetx"));
    let areaSet = smartshiftSet.areaSet;
    if (areaSet.length == 0 || smartshiftSet.smartType == 0) {
        return isOccu;  //过滤未启用SmartShift
    }

    for (let i = 0; i < areaSet.length; i++) {
        if (areaSet[i].action == 0) {
            continue;
        }

        if (areaSet[i].address == parseInt(areaNo, 16) && areaSet[i].action > action) {
            isOccu = true;
        }
    }

    if (isOccu) {
        meshLog("wjk isOccu");
        lightControlShowSmartshiftOccu(areaNo, action);
    }

    return isOccu;
}


export const smartshiftApplyJudgeScheduleIsWork = () => {
    let smartshiftSet = JSON.parse(SPGet("smartshiftSetx"));
    if (smartshiftSet.areaSet.length == 0) {
        return [];
    }

    let scheduleSet = JSON.parse(SPGet("scheduleSetx"));
    let scenesSet = JSON.parse(SPGet("scenesSetx"));

    let arrSche = [];
    let countSche = 0;
    for (let i = 0; i < scheduleSet.length; i++) {
        let mDevice = '[]';
        if (scheduleSet[i].isApply) {
            if (scheduleSet[i].scheType == 0) {
                if (scheduleSet[i].colorStatus.isOnOff) {
                    mDevice = scheduleSet[i].devices;
                }
            } else {
                for (let m = 0; m < scenesSet.length; m++) {
                    if (scenesSet[m].sceneID == scheduleSet[i].scene_sceneNo) {
                        mDevice = scenesSet[m].devices;
                    }
                }
            }
        }
        if (mDevice.length > 2) {
            let areaArr = getDeviceOccupyAreas(mDevice); //device占用的area集合
            for (let n = 0; n < areaArr.length; n++) {
                let isAdd = true;
                for (let k = 0; k < arrSche.length; k++) {
                    if (areaArr[n] == arrSche[k]) {
                        isAdd = false;
                    }
                }
                if (isAdd) {
                    arrSche[countSche] = areaArr[n];
                    countSche++;
                }
            }
        }

    }
    meshLog("wjk arrSche:", arrSche);
    let arrOcc = getOccuAreas(smartshiftSet.areaSet, arrSche);

    return arrOcc;
}

export const smartshiftApplyJudgeSensorIsWork = () => {
    let smartshiftSet = JSON.parse(SPGet("smartshiftSetx"));
    if (smartshiftSet.areaSet.length == 0) {
        return [];
    }

    let sensorSet = JSON.parse(SPGet("sensorSetx"));
    let arrSen = [];
    let countSen = 0;
    for (let i = 0; i < sensorSet.length; i++) {
        // meshLog("wjk sensorSet:",i,' :',sensorSet[i]);
        if (sensorSet[i].address < parseInt('C000', 16)) {
            continue;
        }
        let isAdd = false;
        if (sensorSet[i].hasOwnProperty('motionSensor')) {
            //sensorModel 0-disable 1-occupancy 2-vacancy
            let sensorMode = sensorSet[i].motionSensor.sensorMode;
            let occu = sensorSet[i].motionSensor.occupiedLevel;
            let vacan = sensorSet[i].motionSensor.vacantLevel;
            let isIndependent = sensorSet[i].motionSensor.isIndependent;
            let standby = false;
            if (sensorSet[i].motionSensor.hasOwnProperty('standbyTimeout')) {
                if (sensorSet[i].motionSensor.standbyTimeout > 0) {
                    standby = true;
                }
            }
            if (sensorMode == 1 || isIndependent > 0) {
                if (occu > 0 || vacan > 0 || standby) {
                    isAdd = true;
                }
            } else if (sensorMode == 2) {
                if (vacan > 0 || standby) {
                    isAdd = true;
                }
            }
        }

        if (isAdd) {
            arrSen[countSen] = sensorSet[i].address.toString(16);
            countSen++;
        }
    }
    meshLog("wjk arrSen:", arrSen);
    let arrOcc = getOccuAreas(smartshiftSet.areaSet, arrSen);
    return arrOcc;
}

function getOccuAreas(areaSet, arrArea) {
    let arrOcc = [];
    let countOcc = 0;
    for (let i = 0; i < areaSet.length; i++) {
        let isAdd = false;
        for (let j = 0; j < arrArea.length; j++) {
            if (areaSet[i].address == parseInt(arrArea[j], 16)) {
                isAdd = true;
            }
        }
        if (isAdd) {
            arrOcc[countOcc] = areaSet[i].address;
            countOcc++
        }

    }
    meshLog("wjk arrOcc:", arrOcc);
    return arrOcc;
}


function getOccuAreaName(areaSetx, arrOcc) {
    meshLog("wjk arrOcc:", arrOcc);
    let info = '';
    for (let i = 0; i < arrOcc.length; i++) {
        for (let j = 0; j < areaSetx.length; j++) {
            if (arrOcc[i] == parseInt(areaSetx[j].areaNo, 16)) {
                if (i == arrOcc.length - 1) {
                    info = info + areaSetx[j].name + ' ';
                } else {
                    info = info + areaSetx[j].name + ' , ';
                }
                break;
            }
        }
    }
    return info;
}



export const getDimTemErrorInfo = (sensOcc, scheOcc, isTem) => {
    let temInfo = '';
    let dimInfo = '';

    let smartshiftSet = JSON.parse(SPGet("smartshiftSetx"));
    meshLog("wjk smartType:", smartshiftSet.smartType);
    meshLog("wjk areaSet:", smartshiftSet.areaSet);
    let areaSetx = JSON.parse(SPGet("areaSetx"));
    let info = '';
    if (smartshiftSet.smartType == 1) {
        //is1Contain0
        let occu0 = [];
        let count0 = 0;
        for (let i = 0; i < smartshiftSet.areaSet.length; i++) {
            if (smartshiftSet.areaSet[i].action == 0) {
                occu0[count0] = smartshiftSet.areaSet[i].address;
                count0++;
            }
        }
        if (count0 > 0) {
            info = getOccuAreaName(areaSetx, occu0);
            if (count0 == 1) {
                info = "SmartShift of " + info + " has been disabled";
            } else {
                info = "SmartShift of " + info + " have been disabled";
            }
            temInfo = info;
        }

    } else if (smartshiftSet.smartType == 2) {
        //is2Contain0  //is2Contain1
        let occu0 = [];
        let count0 = 0;
        let occu1 = [];
        let count1 = 0;
        for (let i = 0; i < smartshiftSet.areaSet.length; i++) {
            if (smartshiftSet.areaSet[i].action == 0) {
                occu0[count0] = smartshiftSet.areaSet[i].address;
                count0++;
            } else if (smartshiftSet.areaSet[i].action == 1) {
                occu1[count1] = smartshiftSet.areaSet[i].address;
                count1++;
            }
        }
        if (count0 > 0) {
            info = getOccuAreaName(areaSetx, occu0);
            if (count0 == 1) {
                info = "SmartShift of " + info + " has been disabled";
            } else {
                info = "SmartShift of " + info + " have been disabled";
            }
            temInfo = info;
        }

        if (count1 > 0) {
            info = getOccuAreaName(areaSetx, occu1);
            if (count1 == 1) {
                info = "Automatic Dimming of " + info + " has been disabled";
            } else {
                info = "Automatic Dimming of " + info + " have been disabled";
            }
            dimInfo = info;
        }
    }

    if (sensOcc.length > 0) {
        info = getOccuAreaName(areaSetx, sensOcc);
        info = 'Automatic Dimming is disabled because of a sensor is controlling ' + info + '.'
        dimInfo = info;
    }

    if (scheOcc.length > 0) {
        info = getOccuAreaName(areaSetx, scheOcc);
        info = 'SmartShift of ' + info + ' is disabled because a schedule is controlling it.'
        temInfo = info;
        dimInfo = info;
    }

    meshLog("wjk temInfo:", temInfo);
    meshLog("wjk dimInfo:", dimInfo);

    if (isTem) {
        return temInfo;
    } else {
        return dimInfo;
    }

}


export const getSmartshiftTimerStr = (smartshiftSet) => {


}


export const syncDataBySmartshift = (areas, dim, tem) => {
    if (areas.length == 0) {
        return;
    }

    let count = 0;
    let lists = [];
    for (let i = 0; i < areas.length; i++) {
        lists[count] = { addr: areas[i].address, dim: dim, tem: tem };
        count++;
    }

    meshLog("wjk syncDataBySmartshift:", lists);
    //addr为组地址
    //[{addr: 49168, dim: 40, tem: 3500}]

    for (let i = 0; i < lists.length; i++) {

        let areaNo = lists[i].addr.toString(16).toUpperCase()
        let areaSetx = JSON.parse(SPGet("areaSetx"));

        let areaitem;
        let temx = 0
        let dimx = 0
        for (let index = 0; index < areaSetx.length; index++) {
            if (areaSetx[index].areaNo == areaNo) {
                areaitem = areaSetx[index]
            }
        }
        if (lists[i].tem == 0) {
            dimx = Math.round(lists[i].dim / 100 * 65535)
            temx = areaitem.tem
        } else if (lists[i].dim == 0) {
            dimx = areaitem.dim
            temx = lists[i].tem
        } else {
            temx = lists[i].tem
            dimx = Math.round(lists[i].dim / 100 * 65535)
        }
        // meshLog('temx', temx);
        // meshLog('dimx', dimx);
        areaitem.tem = temx
        areaitem.dim = dimx
        areaitem.rgbH = 0
        //saveAreaInfor(areaitem)
        //修改localStorage上deviceSetx的数据
        let deviceSetx = JSON.parse(SPGet("deviceSetx"));
        let devSetItem = [];
        let devtemx = 0
        let devdimx = 0
        //查询属于这个areaNo的 device
        devSetItem = deviceSetx.filter((item) => {
            if (item.areaNo == areaNo && item.isOnline == '01') {

                if (lists[i].tem == 0) {
                    devdimx = Math.round(lists[i].dim / 100 * 65535)
                    devtemx = item.tem
                } else if (lists[i].dim == 0) {
                    devdimx = item.dim
                    devtemx = lists[i].tem
                } else {
                    devtemx = lists[i].tem
                    devdimx = Math.round(lists[i].dim / 100 * 65535)
                }

                item.tem = devtemx
                item.dim = devdimx
                item.rgbH = 0
                return item
            }
        })
        meshLog('devSetItem', devSetItem);
        //saveLightInfor(devSetItem)

        //查询属于这个areaNo的 subgroup
        let groupSetx = JSON.parse(SPGet("groupSetx"));
        let groupSetItem = [];
        let grouptemx = 0
        let groupdimx = 0
        groupSetItem = groupSetx.filter((item) => {
            if (item.areaNo == areaNo && item.isOnline == '01') {
                if (lists[i].tem == 0) {
                    groupdimx = Math.round(lists[i].dim / 100 * 65535)
                    grouptemx = item.tem
                } else if (lists[i].dim == 0) {
                    groupdimx = item.dim
                    grouptemx = lists[i].tem
                } else {
                    grouptemx = lists[i].tem
                    groupdimx = Math.round(lists[i].dim / 100 * 65535)
                }

                item.tem = grouptemx
                item.dim = groupdimx
                item.rgbH = 0
                return item
            }
        })

        //meshLog('groupSetItem', groupSetItem);
        //saveGroupSetInfor(groupSetItem)


    }
    // PubSub.publish('data', areaitem)//返回给home界面
    // PubSub.publish('temarea', areaNo)//返回给area组内
    // PubSub.publish('areaCCTbackgroudColol', areaitem)//返回给area组内
    // PubSub.publish('areacctzhuangtai', areaitem)//返回给组内修改组的背景条和颜色
}
